<?php
declare (strict_types=1);

namespace app\api\controller\v2;

use app\common\model\Datum;

class SentRead extends BaseController
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        return sucessMsg(200, '获取成功', [
            'neirong' => $this->cont(),   //获取短句认读信息
        ]);
    }

    /**
     * 获取短句认读内容
     *
     * @return \think\Response
     */
    public function cont()
    {
        $page = input('page/d', 1);
        $pageSize = 1;
        $id = input('id/d');
        $model = new Datum();
        if (empty($id)){
            $unit['unit_id'] =1;
        }
        $unit = $model->where('id',$id)->field('unit_id,datum_cate_id')->find();
        $data = $model->where($unit)->page($page, $pageSize)->select();
        if (!empty($data)) $data->toArray();
        foreach ($data as &$v) {
            if (!empty($v['image'])) $v['image'] = config("app.domain_url") . $v['image'];
            if (!empty($v['video'])) $v['video'] = config("app.domain_url") . $v['video'];
        }
        $rows = count($model->where($unit)->select()->toArray());
        $maxPage = ceil($rows / $pageSize);
        if ($page>$maxPage){
            $unit['unit_id'] = $unit['unit_id']+1;
            //dd($unit);
            $data = $model->where($unit)->page(1, $pageSize)->select();
        }
        return ['data' => $data, 'total' => $rows, 'maxPage' => $maxPage];
    }
}
